‹header›
Click to edit Master text styles
Second level
Third level
Fourth level
Fifth level
‹date/time›
‹footer›
‹#›
When a programmer is crafting a program, it is good practice to break the program down into pieces that can be thought of independently. Once the program has been completed, we can think of its execution as being a series of these pieces that work together in a certain sequence. These pieces then pass the control of the program between each other. While one piece has the control, the other pieces are inactive. This is known as the flow of control in a program.
control statements
Control Statements, then, are ways for a programmer to control what pieces of the program are to be executed at certain times. The syntax of Control statements are very similar to regular english, and are very similar to choices that we make every day. There are two basic types of control statements: branching statements and loops.
Note that the pseudocode statement end if means "end the previous if statement." This is to make it clear what statements are inside the if statement and what statements are outside of the if statement. Depending on a certain condition a certain series of events will be executed. Another type of branching statement is called a switch statement. A switch statement is just a shorter way of writing a lot of if statements. Switch statements will be explained a bit further.
In the last example there is the else statement. The code after it (whether a single line or code between brackets) is executed if the IF statement is FALSE.
One use for else is if there are two conditional statements that will both evaluate to true with the same value(but only at times. For example, x<30 and x<50 will both return true if x is less than 30 but not otherwise), but you wish only one of them to be checked. You can use else after the if statement. If the if statement was true the else statement will not be checked. It is possible to use numerous else statements followed by if statements.
In this case condition is equal to "(x > y)" which is equal to "(3 > 4)" which is a false statement. So the code within the else clause will be executed. The output of this program will be: x is smaller than y If instead the value for x was 6 and the value for y was 2, then condition would be "(6 > 2)" which is a true statement and the output of the program would be: x is bigger than y
Now there is one control statement that is inside of another control statement. This is known as nesting.
This is used to decide whether to do something at a special point, or to decide between two courses of action. Note: In C, a semicolon is a statement terminator, not a statement separator – so it MUST be used before else in a case like this.
Switch case statements are a substitute for long if statements. The basic format for using switch case is outlined above.
The expression or variable has a value. The case says that if it has the value of whatever is after that cases then do whatever follows the colon. The break is used to break out of the case statements. Break is a keyword that breaks out of the code block, usually surrounded by braces, which it is in. In this case, break prevents the program from testing the next case statement also.

Switch case serves as a simple way to write long if statements. Often it can be used to process input from a user.
Switch case statements are a substitute for long if statements. The basic format for using switch case is outlined above.
The expression or variable has a value. The case says that if it has the value of whatever is after that cases then do whatever follows the colon. The break is used to break out of the case statements. Break is a keyword that breaks out of the code block, usually surrounded by braces, which it is in. In this case, break prevents the program from testing the next case statement also.

Switch case serves as a simple way to write long if statements. Often it can be used to process input from a user.
This program will not compile yet, but it serves as a model (albeit simple) for processing input.

If you do not understand this then try mentally putting in if statements for the case statements. Note that using return in the middle of main will automatically end the program. Default simply skips out of the switch case construction and allows the program to terminate naturally. If you do not like that, then you can make a loop around the whole thing to have it wait for valid input. I know that some functions were not prototyped. You could easily make a few small functions if you wish to test the code.
 
Now there is one control statement that is inside of another control statement. This is known as nesting.
The basic structure is...WHILE(true) then execute all the code in the loop. The true represents a boolean expression which could be x==1 or while(x!=7) (x does not equal 7). It can be any combination of boolean statements that are legal. Even, (while x==5 || v==7) which says execute the code while x equals five or while v equals 7.
Comment to example:
1.Don't forget to declare variables
2.While x is less than 100 do
3. x++; Adds 1 to x every time it repeats, in for loops the loop structure allows this to be done in the structure
Comment to example: Loop while x is not zero, ut first execute the code in the section. (It outputs "Hello..." once.
Keep in mind that you must include a trailing semi-colon after while in the above example. Notice that this loop will also execute once, because it automatically executes before checking the truth statement.
The sense of the condition is the opposite from Pascal. C is consistent about always requiring compound statements to be surrounded by braces.
Control statements allow a programmer to craft a program so that certain parts of code execute multiple times, or not at all based on the current state of the program. Control statements are the most basic form of logical control within a program.
Now there is one control statement that is inside of another control statement. This is known as nesting.
Comment to example: The loop goes while x<100, and x increases by one every loop. Keep in mind that the loop condition checks the conditional statement before it loops again. Consequently, when x equals 100 the loop breaks outputting x.
Example 3: Finds first element of x[] that is 0; stops if all n elements are examined without finding one.